package com.eric.windows;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.WindowManager;
import android.widget.Toast;

import com.eric.windows.service.FloatWindowService;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    public final static int SYSTEM_ALERT_WINDOW_REQUEST_CODE = 0x1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (Build.VERSION.SDK_INT >= 23) {
            if (! Settings.canDrawOverlays(MainActivity.this)) {
                Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                        Uri.parse("package:" + getPackageName()));
                startActivityForResult(intent,SYSTEM_ALERT_WINDOW_REQUEST_CODE);
            }
        }else {
            Intent intent = new Intent(MainActivity.this, FloatWindowService.class);
            startService(intent);
        }

        final WindowManager wm = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
        //不好使无效
//        PermissionsManager.getInstance().requestPermissionsIfNecessaryForResult(this,
//                new String[]{Manifest.permission.SYSTEM_ALERT_WINDOW}, new PermissionsResultAction() {
//            @Override
//            public void onGranted() {
//                Intent intent = new Intent(MainActivity.this, FloatWindowService.class);
//                startService(intent);
//            }
//            @Override
//            public void onDenied(String permission) {
//                Toast.makeText(MainActivity.this, "请授权", Toast.LENGTH_SHORT).show();
//
//            }
//        });

        try {


            PackageManager  packageManager = getPackageManager();
            ApplicationInfo applicationInfo = packageManager.getApplicationInfo(getPackageName(),PackageManager.GET_META_DATA);
            String[] requestedPermissions = packageManager.getPackageInfo(getPackageName(),PackageManager.GET_PERMISSIONS).requestedPermissions;
            PackageInfo packageInfo  =  packageManager.getPackageInfo(getPackageName(),PackageManager.GET_ACTIVITIES);

            List<PackageInfo> allPackageInfos = packageManager.getInstalledPackages(PackageManager.GET_ACTIVITIES);// 定义系统安装软件信息包 
            for (PackageInfo pInfo : allPackageInfos){ // 循环取出所有软件信息 
                ApplicationInfo appInfo = pInfo.applicationInfo;// 得到每个软件信息
                Log.e("===========>",appInfo.packageName);
             }


            ActivityInfo[] actInfo = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_ACTIVITIES).activities;
            ActivityInfo actInfo2 = getPackageManager().getActivityInfo(getComponentName(),PackageManager.GET_META_DATA);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        queryAppInfo();

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == SYSTEM_ALERT_WINDOW_REQUEST_CODE) {
            if (Build.VERSION.SDK_INT >= 23) {
                if (!Settings.canDrawOverlays(this)) {
                    Toast.makeText(MainActivity.this, "请授权", Toast.LENGTH_SHORT).show();
                }else {
                    Intent intent = new Intent(MainActivity.this, FloatWindowService.class);
                    startService(intent);
                }
            }
        }
    }





    // 获得所有启动Activity的信息，类似于Launch界面
    public void queryAppInfo() {
        PackageManager pm = this.getPackageManager(); // 获得PackageManager对象
        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        // 通过查询，获得所有ResolveInfo对象.
        List<ResolveInfo> resolveInfos = pm.queryIntentActivities(mainIntent, PackageManager.MATCH_DEFAULT_ONLY);
        // 调用系统排序 ， 根据name排序
        // 该排序很重要，否则只能显示系统应用，而不能列出第三方应用程序
        Collections.sort(resolveInfos,new ResolveInfo.DisplayNameComparator(pm));
        for (ResolveInfo reInfo : resolveInfos) {
            String activityName = reInfo.activityInfo.name; // 获得该应用程序的启动Activity的name
            String pkgName = reInfo.activityInfo.packageName; // 获得应用程序的包名
            String appLabel = (String) reInfo.loadLabel(pm); // 获得应用程序的Label
            Drawable icon = reInfo.loadIcon(pm); // 获得应用程序图标
            // 为应用程序的启动Activity 准备Intent
            Intent launchIntent = new Intent();
            launchIntent.setComponent(new ComponentName(pkgName, activityName));
            Log.e("===========>",appLabel + " activityName---" + activityName
                    + " pkgName---" + pkgName);
        }
    }
}
